import {Component, Input, OnInit} from '@angular/core';
import {GooTypeLevel2, GooTypeLevel2Condition} from '../../goo-type-level2.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyGooTypeLevel2Component} from '../modify-goo-type-level2.component';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {GooTypeLevel1Condition} from '../../../goo-type-level1/goo-type-level1.type';

@Component({
    selector: 'app-main-modify-goo-type-level2',
    templateUrl: './main-modify-goo-type-level2.component.html',
    styleUrls: ['./main-modify-goo-type-level2.component.css']
})
export class MainModifyGooTypeLevel2Component implements OnInit {

    errRes = new ErrRes();

    domain: GooTypeLevel2 = new GooTypeLevel2();

    extend = {
        domain: {
            serLevel1: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '一级分类类型',
                change: (event) => {
                    console.log(event);
                }
            },
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: GooTypeLevel2Condition = new GooTypeLevel2Condition();
            condition.uuid = uuid;
            Services.gooTypeLevel2.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.gooTypeLevel2.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.gooTypeLevel2.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyGooTypeLevel2: ModifyGooTypeLevel2Component, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyGooTypeLevel2.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        Services.gooTypeLevel1.list(new GooTypeLevel1Condition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.serLevel1.items = [];
                item.page.content.forEach(level1 => {
                    this.extend.domain.serLevel1.items.push({id: level1.uuid, name: level1.serTypeName});
                });
            }
        });
    }
}
